package com.hd.crdao.activity;

import android.os.Handler;
import android.view.View;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.ConsumerDetaiAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.DetailsBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/6/30 0030.
 *
 * 消费明细
 */
public class ConsumerDetailsActivity extends BaseActivity {
    @BindView(R.id.head)
    ExpandHeadView mHead;
    @BindView(R.id.listview)
    MyListView mListview;
    public String mBusinessid;
    public ConsumerDetaiAdapter mConsumerDetaiAdapter;

    public Handler mHandler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_consumer_details;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initListener() {
        mHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListview.setonRefreshListener(new MyListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new LoadRunnale(), 1500);
            }
        });
    }

    @Override
    protected void initData() {
        mBusinessid = SPUtils.getString("businessid", "");
        loadData();

    }

    private void loadData() {
        try{
            OkHttpUtils.post().url(Constants.CONSUMER_DETAILS_URL).addParams("sign",Constants.SIGN).addParams("carBusinessId", mBusinessid)
                    .addParams("serviceTypeId","1").addParams("plMethod","ZW").build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("加载数据失败,请检查网络...");
                }

                @Override
                public void onResponse(String response, int id) {
                    Gson gson= new Gson();
                    DetailsBean detailsBean = gson.fromJson(response, DetailsBean.class);
                    List<DetailsBean.DataBean> data = detailsBean.getData();
                    mConsumerDetaiAdapter = new ConsumerDetaiAdapter(data);
                    mListview.setAdapter(mConsumerDetaiAdapter);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    class LoadRunnale implements Runnable {
        @Override
        public void run() {
            //加载数据
            loadData();
            if(mConsumerDetaiAdapter !=null){
                mConsumerDetaiAdapter.notifyDataSetChanged();
            }
            mListview.onRefreshComplete();
        }
    }
}
